package de.ret_world.openrpgstudio.editor.gui;

import buoy.event.CommandEvent;
import buoy.event.ValueChangedEvent;
import buoy.event.WindowClosingEvent;
import buoy.widget.*;
import de.ret_world.openrpgstudio.editor.Media;
import de.ret_world.openrpgstudio.editor.Project;
import de.ret_world.openrpgstudio.editor.helpers.Internationalization;
import java.awt.Insets;
import java.awt.Rectangle;
import java.nio.file.Path;
import java.util.ArrayList;

/**
 * This is a dialog called to choose any media object.
 *
 * @author Emu
 */
public class MediaChooseDialog {

    private WindowWidget parent;
    private BDialog dialog;
    private BComboBox cbMedia;
    private Media media = null;
    private ImagePreviewWidget ipw;
    private AudioPreviewWidget apw;

    public MediaChooseDialog(WindowWidget parent) {
        this.parent = parent;
    }

    /**
     * Shows the Dialog.
     */
    public Media showDialog(int type) {
        Internationalization intn = Internationalization.getInstance();
        Project project = Project.getInstance();
        LayoutInfo li = new LayoutInfo(LayoutInfo.WEST, LayoutInfo.HORIZONTAL, new Insets(5, 5, 5, 5), null);

        dialog = new BDialog(parent, intn.getString("DIALOG.CHOOSEMEDIA"), true);
        dialog.addEventLink(WindowClosingEvent.class, this, "close");

        ColumnContainer cc = new ColumnContainer();

        cc.add(new BLabel(intn.getString("MM.ID")), li);

        ArrayList<Media> lst = new ArrayList<>();
        for (Media m : project.getMediaManager().getMediaMap().values()) {
            if (m.getType() == type) {
                lst.add(m);
            }
        }

        cbMedia = new BComboBox(lst);
        cbMedia.addEventLink(ValueChangedEvent.class, this, "selectionChanged");
        cc.add(cbMedia, li);

        if (type == Media.IMAGE) {
            ExplicitContainer ec = new ExplicitContainer();
            ipw = new ImagePreviewWidget(true);
            ec.add(ipw, new Rectangle(250, 200));
            cc.add(ec, new LayoutInfo(LayoutInfo.CENTER, LayoutInfo.BOTH, new Insets(5, 5, 5, 5), null));
        } else {
            boolean continouus = false;
            if (type == Media.MUSIC) {
                continouus = true;
            }
            apw = new AudioPreviewWidget(continouus);
            cc.add(apw, new LayoutInfo(LayoutInfo.EAST, LayoutInfo.BOTH, new Insets(5, 5, 5, 5), null));
        }

        BorderContainer bc2 = new BorderContainer();
        BButton btn = new BButton(intn.getString("OK"));
        btn.addEventLink(CommandEvent.class, this, "ok");
        if (lst.isEmpty()) {
            btn.setEnabled(false);
        }
        bc2.add(btn, BorderContainer.EAST, li);
        btn = new BButton(intn.getString("CANCEL"));
        btn.addEventLink(CommandEvent.class, this, "close");
        bc2.add(btn, BorderContainer.CENTER, li);
        cc.add(bc2, new LayoutInfo(LayoutInfo.EAST, LayoutInfo.NONE, new Insets(5, 5, 5, 5), null));

        selectionChanged();

        dialog.setContent(cc);
        dialog.pack();
        dialog.setResizable(false);
        dialog.setVisible(true);

        return media;
    }

    private void selectionChanged() {
        if (cbMedia.getSelectedValue() != null) {
            Project project = Project.getInstance();
            Media m = (Media) cbMedia.getSelectedValue();
            Path p = project.getFolder("root").resolve(m.getFile());
            if (m.getType() == Media.IMAGE) {
                ipw.setImage(p);
            } else {
                apw.setAudio(p);
            }
        }
    }

    private void close() {
        dialog.dispose();
    }

    private void ok() {
        media = (Media) cbMedia.getSelectedValue();
        dialog.dispose();
    }
}
